var arr = parseURL(window.location.href);

function parseURL(url) {
	if (url.indexOf("?") == -1) {
		return [];
	}
	var url = url.split("?")[1];
	var para = url.split("&");
	var res = {};
	var arr = [];
	for (var i = 0; i < para.length; i++) {
		arr = para[i].split("=");
		res[arr[0]] = arr[1];
	}
	return res;
}


var vm = new Vue({
	el: "#app",
	data: {
		startEat: false,
		startMove: false,
		myClass: {
			0: '',
			1: 'eat',
			2: 'meal',
			3: 'eat oen',
			37: 'top top37',
			38: 'top top38',
			39: 'top top39',
			40: 'top top40',
			47: 'end end37',
			48: 'end end38',
			49: 'end end39',
			50: 'end end40',
			5: 'eat top-left',
			6: 'eat top-right',
			7: 'eat bottom-right',
			8: 'eat bottom-left',
		},
		velocitys: [{
				name: "普通级",
				velocity: 225
			},
			{
				name: "冒险级",
				velocity: 150
			},
			{
				name: "勇士级",
				velocity: 100
			},
			{
				name: "王者级",
				velocity: 75
			},
			{
				name: "噩梦级",
				velocity: 55
			},
			{
				name: "地狱级",
				velocity: 30
			}
		],
		x: arr.x ? arr.x : 20,
		y: arr.y ? arr.y : 20,
		velocity: arr.velocity ? arr.velocity : 2,
		tArray: [],
		myArray: [],
		direction: 1,
		isyong: true,
		score: 0,
		meal: [],
		game: {},
		resurgence:3
	},
	created(){
		document.onkeydown = this.keydowns;
	},
	methods: {
		start() {
			this.x = parseInt(this.x);
			this.y = parseInt(this.y);
			//生成随机位置
			this.myArray[0] = {
				x: GetRandomNum(0, this.x - 1),
				y: GetRandomNum(0, this.y - 1),
				direction: this.direction
			};
			this.show();
			this.startEat = true;
		},
		keydowns(e) {
			var code = e.keyCode;
			if(!vm.startEat && code == 13){
				this.start();
			}
			if (vm.startEat && code >= 37 && code <= 40 && code != vm.direction && vm.isyong) {
				if ((vm.direction == 37 && code == 39) || (vm.direction == 39 && code == 37)) {
					return;
				} else if ((vm.direction == 38 && code == 40) || (vm.direction == 40 && code == 38)) {
					return;
				}
				vm.direction = e.keyCode;
				vm.isyong = false;
				if (!vm.startMove) {
					if(vm.meal.length <= 0){
						vm.manufacture(0);
					}
					vm.startGame();
					vm.startMove = true;
				}

			}
		},
		startGame() {
			this.game = window.setInterval(function() {
				vm.isyong = true;
				var next = {
					x: vm.myArray[0].x,
					y: vm.myArray[0].y,
					direction: vm.direction
				}
				vm.nextNum(next);
				if (next.x > vm.x - 1 || next.x < 0 || next.y > vm.y - 1 || next.y < 0) {
					vm.lose();
					return;
				}

				for (var i = 2; i < vm.myArray.length - 1; i++) {
					var obj = vm.myArray[i];
					if (next.x == obj.x && next.y == obj.y) {
						vm.lose();
						return;
					}
				}

				if (vm.tArray[next.y][next.x] == 2) {
					vm.arithmetic(next, true);
					vm.score++;
					if (vm.score > 0 && vm.score % 10 == 0 && vm.score < (vm.x * vm.y / 2)) {
						var i = parseInt(vm.score / 10);
						vm.manufacture(i);
					}
					for (var j = 0; j < vm.meal.length; j++) {
						if (vm.meal[j].x == next.x && vm.meal[j].y == next.y) {
							vm.manufacture(j);
							break;
						}
					}
				} else {
					vm.arithmetic(next, false);
				}
				vm.show();
			}, this.velocitys[this.velocity].velocity);
		},
		arithmetic(next, b) {
			var arr = [next];
			var ll = this.myArray.length;
			ll = b ? ll : ll - 1;
			for (var i = 0; i < ll; i++) {
				arr[i + 1] = this.myArray[i];
			}
			this.myArray = arr;
		},
		lose() {
			window.clearInterval(vm.game);
			vm.startMove = false;
			if(this.resurgence){
				this.resurgence--;
				return;
			}
			alert("你输了,您得分为:" + vm.score + "分");
			window.location.href = "gluttonousSnake.html?velocity=" + this.velocity + "&x=" + this.x + "&y=" + this.y;
		},
		nextNum(next) {
			switch (this.direction) {
				case 37:
					next.x--;
					break;
				case 38:
					next.y--;
					break;
				case 39:
					next.x++;
					break;
				case 40:
					next.y++;
					break;
			}
		},
		show() {
			var arr = new Array(this.y);
			for (var i = 0; i < arr.length; i++) {
				arr[i] = new Array(this.x);
				for (var j = 0; j < arr[i].length; j++) {
					arr[i][j] = 0;
				}
			}
			for (var i = 0; i < this.meal.length; i++) {
				var obj = this.meal[i];
				arr[obj.y][obj.x] = 2;
			}
			for (var i = 0; i < this.myArray.length; i++) {
				var obj = this.myArray[i];
				arr[obj.y][obj.x] = 1;
				if (i == 0) {
					arr[obj.y][obj.x] = obj.direction;
				} else if (i < this.myArray.length - 1) {
					arr[obj.y][obj.x] = this.isdirection(obj, this.myArray[i - 1]);
				} else if (i = this.myArray.length - 1) {
					arr[obj.y][obj.x] = 10 + this.myArray[i - 1].direction;
				}
			}
			this.tArray = arr;
		},
		isdirection(o1, o2) {
			if (o1.direction == o2.direction) {
				return 1;
			}
			if ((o1.direction == 37 && o2.direction == 38) || (o1.direction == 40 && o2.direction == 39)) {
				return 8;
			} else if ((o1.direction == 37 && o2.direction == 40) || (o1.direction == 38 && o2.direction == 39)) {
				return 5;
			} else if ((o1.direction == 39 && o2.direction == 40) || (o1.direction == 38 && o2.direction == 37)) {
				return 6;
			} else if ((o1.direction == 39 && o2.direction == 38) || (o1.direction == 40 && o2.direction == 37)) {
				return 7;
			}
			return 1;
		},
		//生成一个随机色块
		manufacture(j, n) {
			var ob = {};
			ob.x = GetRandomNum(0, this.x - 1);
			ob.y = GetRandomNum(0, this.y - 1);
			if (n > 5) {
				return;
			}
			for (var i = 0; i < this.myArray.length; i++) {
				var obj = this.myArray[i];
				if (ob.x == obj.x && ob.y == obj.y) {
					return vm.manufacture(j, n + 1);
				}
			}
			this.meal[j] = ob;
		}
	}
});

//随机数算法
function GetRandomNum(Min, Max) {
	var Range = Max - Min;
	var Rand = Math.random();
	return (Min + Math.round(Rand * Range));
}
